package cn.devzyh.oms.coms.flow.controller;

import cn.devzyh.oms.common.core.util.R;
import cn.devzyh.oms.common.excel.annotation.ResponseExcel;
import cn.devzyh.oms.common.log.annotation.SysLog;
import cn.devzyh.oms.common.mybatis.domain.PageQuery;
import cn.devzyh.oms.coms.api.flow.entity.Flow;
import cn.devzyh.oms.coms.flow.executor.FlowExecutor;
import cn.devzyh.oms.coms.flow.service.FlowService;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 流程-流程表
 *
 * @author devzyh
 * @date 2024-02-26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/flow")
public class FlowController {

	private final FlowService flowService;

	private final FlowExecutor flowExecutor;

	/**
	 * 分页查询
	 *
	 * @param page 分页对象
	 * @param flow 查询条件
	 * @return R
	 */
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('coms_flow_view')")
	public R getFlowPage(@ModelAttribute PageQuery<Flow> page, @ModelAttribute Flow flow) {
		LambdaQueryWrapper<Flow> wrapper = Wrappers.lambdaQuery();
		return R.ok(flowService.page(page.mybatisPlusPage(), wrapper));
	}

	/**
	 * 通过ID查询
	 *
	 * @param id id
	 * @return R
	 */
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coms_flow_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(flowService.getById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param flow 流程-流程表
	 * @return R
	 */
	@SysLog("新增流程-流程表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('coms_flow_add')")
	public R save(@RequestBody Flow flow) {
		return R.ok(flowService.save(flow));
	}

	/**
	 * 修改数据
	 *
	 * @param flow 流程-流程表
	 * @return R
	 */
	@SysLog("修改流程-流程表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('coms_flow_edit')")
	public R updateById(@RequestBody Flow flow) {
		return R.ok(flowService.updateById(flow));
	}

	/**
	 * 通过ID删除
	 *
	 * @param ids id列表
	 * @return R
	 */
	@SysLog("通过ID删除流程-流程表")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('coms_flow_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(flowService.removeBatchByIds(CollUtil.toList(ids)));
	}

	/**
	 * 导出Excel表格
	 *
	 * @param flow 查询条件
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('coms_flow_export')")
	public List<Flow> export(@ModelAttribute Flow flow) {
		LambdaQueryWrapper<Flow> wrapper = Wrappers.lambdaQuery();
		return flowService.list(wrapper);
	}

	/**
	 * 测试流程
	 *
	 * @param flowId 流程ID
	 * @return R
	 */
	@SysLog("测试流程")
	@PostMapping("/test/{flowId}")
	@PreAuthorize("@pms.hasPermission('coms_flow_test')")
	public R test(@PathVariable Long flowId) {
		return flowExecutor.test(flowId);
	}

	/**
	 * 流程执行
	 *
	 * @param flowId 流程ID
	 * @return R
	 */
	@SysLog("流程执行")
	@PostMapping("/run/{flowId}")
	@PreAuthorize("@pms.hasPermission('coms_flow_run')")
	public R run(@PathVariable Long flowId, @RequestBody Map<String, Object> params) {
		return flowExecutor.run(flowId, params);
	}

}